package org.yankun.exam4j.web.controller.dashboard;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.yankun.exam4j.core.exam.service.ExamService;
import org.yankun.exam4j.entity.Exam;
import org.yankun.exam4j.entity.User;
import org.yankun.exam4j.security.user.service.UserService;
import org.yankun.exam4j.session.SessionManager;
import org.yankun.exam4j.session.UserSessionData;


@Controller("ExamManageController")
@RequestMapping("/dashboard/exam")
public class ExamManageController {
	
	@Resource
	private ExamService examService;
	
	@Resource
	private UserService userService;
	
	@RequestMapping("index")
	public ModelAndView toIndex(HttpServletResponse response,HttpServletRequest request,String email,String password){
		return new ModelAndView("/dashboard/exam/index");
	}
	
	@RequestMapping("toAddExam")
	public ModelAndView toAddExam(HttpServletResponse response,HttpServletRequest request){
		return new ModelAndView("/dashboard/exam/add");
	}
	
	@RequestMapping("doAddExam")
	public String doAddExam(HttpServletResponse response,HttpServletRequest request,Exam exam){
		HttpSession session = request.getSession();
		UserSessionData usd = SessionManager.getSessionData(session);
		
		User user = userService.getUserById(usd.getUserId());
		exam.setUserByCreateUserId(user);
		exam.setUserByUpdateUserId(user);
		exam.setCreateTime(new Date());
		exam.setUpdateTime(new Date());
		examService.addExam(exam);
		
		return "redirect:/dashboard/paper/toAddPaper/"+exam.getUid()+".do";
	}
}